<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Yinhuan;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use App\Admin\Renderable\DeptTable;
use App\Models\Dept;
use Dcat\Admin\Admin;

class YinhuanController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Yinhuan(), function (Grid $grid) {
            $grid->withBorder();


            $grid->selector(function (Grid\Tools\Selector $selector) {
                $selector->selectOne('louzhang', '楼长', [
                    '胥鹏' => '胥鹏',
                    '王明明' => '王明明',
                    '张宝军' => '张宝军',
                    '邵泽浩' => '邵泽浩',
                    '牛延军' => '牛延军',
                    '曾聪' => '曾聪',
                    '王涛广' => '王涛广',
                    '倪意宣' => '倪意宣',
                    '刘玉斌' => '刘玉斌',
                    '孙振华' => '孙振华',
                    '秦证' => '秦证',
                    '郭忠建' => '郭忠建',
                    '陈婷' => '陈婷',
                    '徐志刚' => '徐志刚',
                ]);
                $selector->selectOne('iswancheng', '整改状态', [0 => '未完成' . '(' . \App\Models\Yinhuan::where('iswancheng', 0)->count() . ')', 1 => '已完成' . '(' . \App\Models\Yinhuan::where('iswancheng', 1)->count() . ')']);
            });
            $grid->quickSearch('yinhuanwenti', 'dept.deptname', 'zerenren', 'louzhang')->placeholder('输入部门或隐患或责任人或楼长搜索...');
            // 禁用过滤器按钮
            $grid->disableFilterButton();
            // 禁用刷新按钮
            $grid->disableRefreshButton();
            // 禁用编辑按钮
            $grid->disableEditButton();
            $grid->setDialogFormDimensions('75%', '75%');
            // 显示快捷编辑按钮
            $grid->showQuickEditButton();

            $grid->export();

            $grid->model()->orderBy('id', 'desc');

            // $grid->column('id')->sortable();
            $grid->model()->with(['dept']);
            $grid->column('dept.deptname', '责任部门')->link(function ($value) {
                $deptid = Dept::where('deptname', $value)->first()->id;
                return admin_url('depts/' . $deptid);
            });
            // $grid->column('zerenbumen');
            $grid->column('yinhuanwenti');
            $grid->column('zerenren');
            $grid->column('zhenggaicuoshi');
            $grid->column('zhenggaidate');
            $grid->column('yinhuanpics')->display(function ($imgs) {
                $pics = json_decode($imgs);
                if ($pics) {
                    return $pics[0];
                }
            })->image();
            $grid->column('iswancheng')->switch();
            $grid->column('zhenggaipics')->display(function ($imgs) {
                $pics = json_decode($imgs);
                if ($pics) {
                    return $pics[0];
                }
            })->image();
            // $grid->column('wanchengdate');
            $grid->column('louzhang');
            // $grid->column('created_at');
            $grid->column('created_at', '提交日期')->display(function ($updated_at) {
                return  date('y-m-d', strtotime($updated_at));
            });
            // $grid->column('updated_at', '提交日期')->sortable();
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        $repository = new Yinhuan(['dept']);

        return Show::make($id, $repository, function (Show $show) {

            // $show->field('id');
            // $show->model()->with(['dept']);
            $show->field('dept.deptname', '责任部门');

            $show->field('yinhuanwenti');
            $show->field('zerenren');
            $show->field('zhenggaicuoshi');
            $show->field('zhenggaidate');
            $show->yinhuanpics()->image();
            $show->field('zhenggaidate');
            $show->zhenggaipics()->image();
            $show->iswancheng->using([0 => '未处理', 1 => '已处理'])->dot(
                [
                    0 => 'danger',
                    1 => 'success',
                ],
                'primary' // 第二个参数为默认值
            );
            // $show->field('iswancheng');
            $show->field('louzhang');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Yinhuan(), function (Form $form) {
            $form->display('id');
            $form->selectTable('dept_id', '所属部门：')
                ->title('部门列表')
                ->from(DeptTable::make())
                ->model(Dept::class, 'id', 'deptname')->required()->width(3);

            $form->text('zerenren')->width(3);
            $form->text('yinhuanwenti')->required();

            $form->text('zhenggaicuoshi');
            $form->date('zhenggaidate');
            $form->multipleImage('yinhuanpics')->accept('jpg,png,gif,jpeg')->limit(6)->autoUpload()->maxSize(20480)->saving(function ($files) {
                return json_encode($files);
            });

            $form->switch('iswancheng')->default(0);

            $form->multipleImage('zhenggaipics')->accept('jpg,png,gif,jpeg')->limit(6)->autoUpload()->maxSize(20480)->saving(function ($files) {
                return json_encode($files);
            });;
            $form->date('wanchengdate');
            $form->text('louzhang')->value(Admin::user()->name)->width(2);
        });
    }
}
